package testmyself;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;

public class GCLogAnalysis {
	private static final Random random = new Random();
	
	public static void main(String[]args) {
		long startMillis = System.currentTimeMillis();
		long period = TimeUnit.SECONDS.toMillis(1);
		long endMillis = startMillis + period;
		LongAdder counter = new LongAdder();
		System.out.println("Running...");
		
		int cacheSize=2000;
		Object[] cacheObject = new Object[cacheSize];
		
		while(System.currentTimeMillis()<endMillis) {
			Object garbage = generateGarbage(100*1024);
			counter.increment();
			int randomIndex = random.nextInt(2*cacheSize);
			if(randomIndex<cacheSize) {
				cacheObject[randomIndex]=garbage;
			}
		}
		System.out.println("Ending...,counter="+counter);
	}

	private static Object generateGarbage(int max) {
		int randomSize = random.nextInt(max);
		int type = randomSize%4;
		Object res = null;
		switch(type){
		case 0:
			res = new int[randomSize];
			break;
		case 1:
			res = new byte[randomSize];
			break;
		case 2:
			res = new char[randomSize];
			break;
		case 3:
			res = new double[randomSize];
			break;
		default:
			StringBuilder sb = new StringBuilder();
			String base="sdfaetqwerqwrq343434";
			while(sb.length()<randomSize) {
				sb.append(base);
			}
			res = sb.toString();
			break;
		}
		return res;
	}
}
